package model

import (
	"gf-xb-api/cwt/consts"
	"strings"
)

type FilterGroup[T any] struct {
	Rules []*FilterRule[T] `json:"rules"`

	Groups []*FilterGroup[T] `json:"filterGroup"`

	Operate any `json:"operate"`
}

func (f *FilterGroup[T]) UnmarshallFilterGroupJson(gMap map[string]any) *FilterGroup[T] {

	// 这里我们要进行递归循环解析
	// {"rules":{}, "groups":[], "operate": 1}
	var g = &FilterGroup[T]{}

	for k, v := range gMap {

		switch strings.ToLower(k) {
		case consts.ATTR_RULES:
			var rule = &FilterRule[T]{}
			g.Rules = rule.UnmarshallFilterRuleJson(v)
		case consts.ATTR_GROUPS:
			if v != nil {
				if g.Groups == nil {
					g.Groups = []*FilterGroup[T]{}
				}

				if groups, ok := v.([]interface{}); ok {
					for _, group := range groups {
						if gr, ok := group.(map[string]any); ok {
							tempGroup := f.UnmarshallFilterGroupJson(gr)
							g.Groups = append(g.Groups, tempGroup)
						}
					}
				}
			}
		case consts.ATTR_OPERATE:
			g.Operate = v
		}
	}

	return g
}
